/*
 * OptionalAttributes.java
 * 
 * Created on Oct 17, 2009, 3:45:56 AM
 * 06, April, 2011: Base class no longer inherits from class Table, rather table is now a composition object.
 * 18, November, 2011: Class modified to use TableFactory.
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package portfolio.dbHost.project.attributes;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import misellanea.MiscellaneaDB;
import portfolio.dbHost.Host;
import portfolio.dbHost.dataBases.DBaseFactory;
import portfolio.dbHost.dataBases.dbTable.MysqlTable;
import portfolio.dbHost.dataBases.dbTable.OdbcTable;
import portfolio.dbHost.dataBases.dbTable.Table;
import portfolio.dbHost.dataBases.dbTable.TableFactory;
import portfolio.dbHost.project.Project;

/**
 *
 * @author ACA
 */
public class OptionalAttributes /*extends Table*/ implements AbstractAttribute, MouseListener
{
    private Table table = null;

    static final public String TABLE_NAME      = "optionalattribs";
    static final private String COLUMN_STCKIDX  = "stockIndex";
    static final private String COLUMN_RSKFREE  = "reskfree";
    static final private String TYPE_STCKIDX    = OptionalAttributes.COLUMN_STCKIDX  + " CHAR(20)";
    static final private String TYPE_RSKFREE    = OptionalAttributes.COLUMN_RSKFREE  + " CHAR(20)";
    
    static final public int OPTIONALATTRIBS_SCTKIDX = 0;
    static final public int OPTIONALATTRIBS_RSKFREE = 1;
    static final public int OPTIONALATTRIBS_SIZE    = 2;
    
    private Vector [] vOptionalAttribs = null;

    /**
     * @Overrides parent's method Table.mousePressed 
     */
    int mouseReleased = 1;
    public void mouseClicked(MouseEvent e) {
 //       System.out.println ("OptionalAttributes: [[[[[[[[[[[[[[[[[[[[[[[[[ Mouse cliked ]]]]]]]]]]]]]]]]]]]]]]]]]");
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mousePressed(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName  + ": OptionalAttributes: [[[[[[[[[[[[[[[[[[[[[[[[[ Mouse Pressed ]]]]]]]]]]]]]]]]]]]]]]]]]");
//        System.out.println ("OptionalAttributes: [[[[[[[[[[[[[[[[[[[[[[[[[ Mouse pressed ]]]]]]]]]]]]]]]]]]]]]]]]]");
//        throw new UnsupportedOperationException("Not supported yet.");
    }
    public void mouseReleased(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName  + ": OptionalAttributes: [[[[[[[[[[[[[[[[[[[[[[[[[ Mouse Released ]]]]]]]]]]]]]]]]]]]]]]]]]");
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName  + ": OptionalAttributes: [[[[[[[[[[[[[[[[[[[[[[[[[ Mouse Entered ]]]]]]]]]]]]]]]]]]]]]]]]]");
//        System.out.println ("OptionalAttributes: [[[[[[[[[[[[[[[[[[[[[[[[[ Mouse entered ]]]]]]]]]]]]]]]]]]]]]]]]]");
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName  + ": OptionalAttributes: [[[[[[[[[[[[[[[[[[[[[[[[[ Mouse Exited ]]]]]]]]]]]]]]]]]]]]]]]]]");
//        System.out.println ("OptionalAttributes: [[[[[[[[[[[[[[[[[[[[[[[[[ Mouse exit ]]]]]]]]]]]]]]]]]]]]]]]]]");
//        throw new UnsupportedOperationException("Not supported yet.");
    }
    /**
     * 
     */
    public OptionalAttributes (Project projectParam) throws Exception
    {
////        super (projectParam);
////        super.setName(TABLE_NAME);
//        int iDBType = projectParam.getDBHost().getDBType();
//        if (iDBType == Host.DB_TYPE_MYSQL) {
//            table = new MysqlTable (projectParam);
//        } else if (iDBType == Host.DB_TYPE_ODBC) {
//            table = new OdbcTable (projectParam);
//        } else {
//            throw new Exception ("Unknown Data Base.");
//        }
        TableFactory tableFactory = new TableFactory ();
        table = tableFactory.getDBType(projectParam.getProjectDBase());
        table.setName(OptionalAttributes.TABLE_NAME);

        table.getDBase().setTablesVector();
        Vector vTables = table.getDBase().showTables();
        if (vTables == null || !vTables.contains(OptionalAttributes.TABLE_NAME)) {
            this.create ();
        }
    }
    public void initComponents (String sOptAttrStock, String sOptAttrRiskFree) throws Exception
    {
        
        Vector vValues = new Vector ();
        vValues.add (OptionalAttributes.OPTIONALATTRIBS_SCTKIDX,  sOptAttrStock);
        vValues.add (OptionalAttributes.OPTIONALATTRIBS_RSKFREE,  sOptAttrRiskFree);

        this.insert(vValues);
        this.validate (); //0j0, this validate is not dummy
    }
    /**
     * 
     */
    public DefaultMutableTreeNode processHierarchy() {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode (OptionalAttributes.TABLE_NAME);
        
        DefaultMutableTreeNode nStockIndexCol = new DefaultMutableTreeNode (OptionalAttributes.COLUMN_STCKIDX);
        String sStockIndex = vOptionalAttribs [OptionalAttributes.OPTIONALATTRIBS_SCTKIDX].get(0).toString();
        DefaultMutableTreeNode nStockIndex    = new DefaultMutableTreeNode (sStockIndex);
        nStockIndexCol.add(nStockIndex);
        node.add(nStockIndexCol);
        
        DefaultMutableTreeNode nRiskFreeCol = new DefaultMutableTreeNode (OptionalAttributes.COLUMN_RSKFREE);
        String sRiskFree = vOptionalAttribs [OptionalAttributes.OPTIONALATTRIBS_RSKFREE].get(0).toString();
        DefaultMutableTreeNode nRiskFree    = new DefaultMutableTreeNode (sRiskFree);
        nRiskFreeCol.add(nRiskFree);
        node.add(nRiskFreeCol);
        
        return node;
    }

    public void getPopupOptions(JTree dbTreeParam, int iXPos, int iYPos) 
    {
        JTree dbaseTree = dbTreeParam;       

        JPopupMenu treeOptionsMenu = new JPopupMenu ("Options");
        dbaseTree.add(treeOptionsMenu);
        
        dbaseTree.addMouseListener(this);

        treeOptionsMenu.setLocation (iXPos, iYPos);
        treeOptionsMenu.show (treeOptionsMenu, iXPos, iYPos);
        
        return;
    }
    /**
     *
     * @throws java.lang.Exception
     */
    public void validate() throws Exception {
        vOptionalAttribs = table.selectAllfromTable();
    }

    public String[] get() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    /*
     * 
     */
    public Object getTreeObject(Object[] oPathParam) {
        if (oPathParam == null) {
            return null;
        }
        if (oPathParam.length <2) {
            return null;
        }
        if (!oPathParam [1].toString().equals(OptionalAttributes.TABLE_NAME)) {
           return null;  //error, no database found
        }
        
        return this;
    }

    public void create () throws Exception {
        table.setName (OptionalAttributes.TABLE_NAME);
        table.create (OptionalAttributes.TYPE_STCKIDX + " ," + OptionalAttributes.TYPE_RSKFREE);
    }

    public void insert(Vector vValues) throws Exception {
        Vector vColumns = new Vector ();
        vColumns.add(OptionalAttributes.COLUMN_STCKIDX);
        vColumns.add(OptionalAttributes.COLUMN_RSKFREE);
        
        Vector vValuesTmp = new Vector ();
        vValuesTmp.add (OptionalAttributes.OPTIONALATTRIBS_SCTKIDX,  "'" + vValues.get(OptionalAttributes.OPTIONALATTRIBS_SCTKIDX)  + "'");
        vValuesTmp.add (OptionalAttributes.OPTIONALATTRIBS_RSKFREE,  "'" + vValues.get(OptionalAttributes.OPTIONALATTRIBS_RSKFREE)  + "'");
        
        table.insertSingleRow(vColumns, vValuesTmp);
        table.buildColumns();
    }
    /**
     * 
     */
    static public Vector read (Connection connection, String sDBName)  throws Exception
    {
        System.out.println ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ProjectNew: Read OptionalAttribs >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        if (connection == null)
            throw new Exception ("null connection.");

        ResultSet resultSet = Table.getQuery(connection, sDBName, "*", OptionalAttributes.TABLE_NAME);
        Object [][] OMatrix = Table.transferObjectResultSet2Matrix(resultSet, 0);
 
        String sStockIndex = (String)OMatrix [0][OptionalAttributes.OPTIONALATTRIBS_SCTKIDX];
        String sRiskFree   = (String)OMatrix [0][OptionalAttributes.OPTIONALATTRIBS_RSKFREE];

        Vector vOutVector = new Vector ();
        
//        String sOutArray [] = new String [2];
        vOutVector.add (sStockIndex);
        vOutVector.add (sRiskFree);
        
        return vOutVector;
    }

    public Table getTable() 
    {
        return table;
    }
}
